package com.lihang0328.groupmessageangel;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendMessageActivity extends AppCompatActivity {;
    private List<Map<String,Object>> list;
    private TextView haveChooseNum;
    private Button btn_chanceSendName;
    private Button btn_sendMessage;
    private EditText mEditText;
    private TextView tv_textNumHaveInput;
    private TextView tv_messageMax;
    private TextView tv_nameShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);
        //群发列表
        list=ContextActivity.getChooseNameList();
        //短信内容字节数
        tv_textNumHaveInput=(TextView)findViewById(R.id.tv_textNumHaveInput_sendMActivity);
        //字节数上限，过长要拆分短信内容
        tv_messageMax=(TextView)findViewById(R.id.tv_textNumHini_sendMActivity);
        tv_nameShow=(TextView)findViewById(R.id.tv_nameChooseShow_sendMessageActivity);
        //选择的第一个联系人
        tv_nameShow.setText((String)list.get(0).get("Name") + "同学,");
        haveChooseNum = findViewById(R.id.haveChooseNum_sendMessageActivity);
        haveChooseNum.setText("已选择了"+list.size()+"个联系人");
        //重新选择
        btn_chanceSendName=(Button)findViewById(R.id.btn_chanceSendName_sendMessageActivity);
        btn_chanceSendName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
//                Bundle bundle=new Bundle();
//                bundle.putString("PATH", getIntent().getStringExtra("PATH"));
//                bundle.putString("ORIGIN", getIntent().getStringExtra("ORIGIN"));
//                Intent intent=new Intent(SendMessageActivity.this,ContextActivity.class);
//                intent.putExtras(bundle);
//                startActivity(intent);
            }
        });
        btn_sendMessage=(Button)findViewById(R.id.btn_sendMessage_sendMessageActivity);
        btn_sendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });
        mEditText=(EditText)findViewById(R.id.et_messageText_sendMessageActivity);
        SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
        if(!sp.getString("saveMessageText","000").equals("000")) {
            mEditText.setText(sp.getString("saveMessageText",""));
        }
        //输入字数以70的倍数为上限
        tv_textNumHaveInput.setText(""+calculateLength(mEditText.getText().toString()));
        tv_messageMax.setText("/"+((calculateLength(mEditText.getText().toString())/70)+1)*70);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tv_textNumHaveInput.setText(""+calculateLength(mEditText.getText().toString())+"/");
                tv_messageMax.setText(""+((calculateLength(mEditText.getText().toString())/70)+1)*70);
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    /**
     * 统计字数
     * @param etstring
     * @return
     */
    private int calculateLength(String etstring) {
        etstring="李行同学,"+etstring;
        char[] ch = etstring.toCharArray();
        int varlength = 0;
        for (int i = 0; i < ch.length; i++) {
            int ascii = Character.codePointAt(ch, i);
            //ascii一共有128个，占1字节; 汉字占2字节
            if (ascii >= 0 && ascii <= 128) {
                ++ varlength;
            } else {
                varlength = varlength + 2;
            }
        }
        Log.d("TextChanged", "varlength = " + varlength);
        // varlength = etstring.getBytes(CharSet.forName("GBK")).lenght;// u8中文占3个字节
        return varlength;
    }

    /**
     * 跳转去发送短信
     */
    private void sendMessage(){
        Bundle bundle=new Bundle();
        bundle.putString("text",mEditText.getText().toString());
        Intent intent=new Intent(SendMessageActivity.this,EnsureSendActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

    /**
     * 保存短信编辑内容
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("saveMessageText",mEditText.getText().toString());
        editor.apply();
    }

}
